use reqwest::header::{HeaderMap, HeaderName,HeaderValue};
use tauri::AppHandle;
use tauri_plugin_store::StoreExt;
use serde_json::json;
 
pub fn _string_to_headers(headers_str: &str) -> HeaderMap {
    let mut headers = HeaderMap::new();
    for line in headers_str.lines() {
        let mut parts = line.splitn(2, ':');
        let key = parts.next().expect("missing key");
        let value = parts.next().expect("missing value");
        let key = HeaderName::from_bytes(key.as_bytes()).expect("invalid header name");
        let value = HeaderValue::from_bytes(value.as_bytes()).expect("invalid header value");
        headers.insert(key, value);
    }
    headers
}

use std::collections::HashMap;
 
pub fn _string_to_hashmap<'a>(json_str: &'a str) -> serde_json::Result<HashMap<&'a str, &'a str>> {
    let hashmap = serde_json::from_str(json_str)?;
    Ok(hashmap)
}

pub fn store_save_data(app: AppHandle,key: String,val: String){
    let mut store = app.store_builder("store.bin").build();
    // store.load().expect("Failed to load store from disk");
    let _ = store.set(key, serde_json::json!({ "value": val }));
    store.save().unwrap()
}
pub fn store_delete_data(app: AppHandle,key: String){
    let mut store = app.store_builder("store.bin").build();
    // store.load().expect("Failed to load store from disk");
    let _ = store.delete(key);
    // store.save().unwrap()
}
pub fn _store_get_data(app: AppHandle,key: String)-> String{
    let store = app.store_builder("store.bin").build();
    // store.load().expect("Failed to load store from disk");
    let v = store.get(key);
    if let Some(value) = v {
        value.as_str().unwrap().to_string()
    }else {
        "".to_string()
    }
}